<?php

namespace app\admin\controller;

use app\admin\controller\common\Base;
use think\Session;

class Index extends Base
{
  public function index()
  {
    $this->assign('admin_name',config('webset.ADMIN_NAME'));
    return $this->fetch();
  }
  public function init()
  {


    $json['homeInfo'] = ['title' => '首页', 'href' => '/admin/index/welcome'];
    $json['logoInfo'] = ['title' => config('webset.ADMIN_NAME'), "image" => "/static/lay/images/logose.png", "href" => ""];
    $where['is_del'] = 0;
    if(db('admin')->where(['is_del'=>0,'state'=>1,'id'=>Session::get('admin')])->value('superadmin') != 1){//非超级管理
      $roleid = db('admin')->where(['is_del'=>0,'state'=>1,'id'=>Session::get('admin')])->value('role');
      $power = db('role')->where(['id'=>$roleid])->value('power');
      $power = explode(",", $power);
      $where['id'] =['in',$power];
      $config = [];
    }else{
      $config = config('menu');
    }
   
    $menuinfo = db('menuinfo')->field('id,title,icon,href,is_menu,sort,pid,target')->where(['is_menu' => 1, 'pid' => -1])->where($where)->order('sort desc')->select();
    if (!empty($menuinfo)) {
      foreach ($menuinfo as $k => $v) {
        $child = db('menuinfo')->where(['is_menu' => 1, 'pid' => $v['id']])->where($where)->order('sort desc')->select();
        if ($child) {
          $v['child'] =  $child;
          foreach ($v['child'] as $k2 => $v2) {
            $child2 = db('menuinfo')->where(['is_menu' => 1, 'pid' => $v2['id']])->where($where)->order('sort desc')->select();
            if ($child2) {
              $v2['child'] = $child2;
            }
            $v['child'][$k2] = $v2;
          }
        }
        $menuinfo[$k] = $v;
      }
    }
    if($config)array_push($menuinfo, $config);
    $json['menuInfo'] = $menuinfo;
    return $json;
  }

  public function welcome()
  {
    return $this->fetch();
  }
  
}
